import 'package:flutter/material.dart';

class FormValidateApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: MyFormStateWidget(),
      ),
    );
  }
}

class MyFormStateWidget extends StatefulWidget {
  MyFormStateWidget({Key key}):super(key:key);

  @override
  _MyFormStateWidgetState createState() => _MyFormStateWidgetState();
}

class _MyFormStateWidgetState extends State<MyFormStateWidget> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all( 20),
      child: Form(
        key: _formKey,
          child: Container(
            padding:const EdgeInsets.only(top: 20),
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(
                        icon: Icon(Icons.edit),
                        suffixIcon: IconButton(icon:Icon(Icons.keyboard_arrow_down),onPressed: (){
                          FocusScope.of(context).requestFocus(new FocusNode());
                        },),
                        hintText: '输入姓名',
                        helperText: '名字必填项',
                      ),
                      validator: (value){
                        if(value.isEmpty){
                          return '请输入中文';
                        }
                      },
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8.0),
                      child: RaisedButton(onPressed: (){
                        if(_formKey.currentState.validate()){
                          print('ok');
                        }
                      },child: Text('提交')),
                    ),
                    Padding(
                      padding:const EdgeInsets.all(10),
                      child: InkWell(
                        onTap: () {
                          setState(() { print("gesture detector"); });
                          Future<DateTime> selectedDate = showDatePicker(
                            context: context,
                            initialDate: DateTime.now(),
                            firstDate: DateTime(2018),
                            lastDate: DateTime(2030),
                            builder: (BuildContext context, Widget child) {
                              return Theme(
                                data: ThemeData.dark(),
                                child: child,
                              );
                            },
                          );
                        selectedDate.then((value){
                          print(value);
                        });
                        },
                        child: Container(
                          child: Text('GestureDector'),
                        ),
                      ),
                    )
                  ],
              ),
            ),
          ),
      ),
    );
  }
}

